package com.mmy.controller.line;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.FileUtils;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.group.TGroupNotice;
import com.mmy.domain.line.VLineBase;
import com.mmy.pojo.line.GroupForm;
import com.mmy.service.ServiceCommon;
import com.mmy.service.group.impl.GroupNoticeServiceImpl;
import com.mmy.service.group.impl.GroupProjectServiceImpl;
import com.mmy.service.impl.line.LineBaseServiceImpl;
import com.mmy.util.CodeUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @vesion 
 * @description 
 * @author liys
 * @date 2021-7-6
 */

@Controller
@Slf4j
public class LineGroupController {
	
	@Value("${web.upload-path}")
	private String userPath;
	@Autowired
	ServiceCommon common;
	@Autowired
	GroupProjectServiceImpl projectService; 
	@Autowired
	GroupNoticeServiceImpl noticeService;
	@Autowired
	LineBaseServiceImpl lineService;
	
	/**
	  * @descr 跳转--出团首页
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-7-5
	 */
	@RequestMapping("/setgroup")
	public String setGroup() {
		
		return "webbackground/tourist/GroupIndex";
	}
	
	/**
	  * @descr 跳转--出团表单填写页
	  * @param body
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-5-19
	 */
	@RequestMapping("/linegroup")
	public String toLineGroup(HttpServletRequest request, Model model) {
		
		String lineID = request.getParameter("iLineID");
		int iLineID = Integer.parseInt(lineID);
		VLineBase base = lineService.findByLineID(iLineID);
		model.addAttribute("base", base);
		return "webbackground/tourist/GroupProject";
	}
	
	/**
	  * @descr 异步--出团表单提交保存
	  * @param body
	  * @return str
	  * @throws 
	  * @author liys
	  * @date 2021-5-19
	 */
	@RequestMapping("/grouppost")
	@ResponseBody
	public String groupPost(@RequestBody String ff,HttpSession session) {
		
		int iOrgID = CodeUtil.getOrgID(session);
		log.info("iOrgID = " + iOrgID);
		GroupForm groupForm = JSON.parseObject(ff,GroupForm.class);
		projectService.save(groupForm,iOrgID);
		return "";
	}
	
	
	/**
	  * @descr 异步--出团通知提交保存
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-7-27
	 */
	@RequestMapping("/noticePost")
	public String noticePost(MultipartHttpServletRequest request) {
		
		TGroupNotice no = new TGroupNotice(); 
		no.setTeamID(Integer.parseInt(request.getParameter("iTeamID")));
		no.setGuideID(Integer.parseInt(request.getParameter("iGuideID")));
		no.setDeliver(request.getParameter("cDriver"));
		no.setDeliverTel(request.getParameter("cDriverTel"));
		no.setPlaceID(request.getParameter("iPlaceID"));
		no.setTime(request.getParameter("cTime"));
		no.setSign(request.getParameter("cSign"));
		no.setPlaneOutPlanID(Integer.parseInt(request.getParameter("iPlaneOutPlanID"))); 
		no.setPlaneIntPlanID(Integer.parseInt(request.getParameter("iPlaneIntPlanID"))); 
		no.setPickUp(request.getParameter("cPickUp"));
		no.setPickUpTel(request.getParameter("cPickUpTel"));
		
		MultipartFile file = request.getFile("file");
		String path = userPath;
		String cNoticePath = FileUtils.upload(file, path);
		no.setNoticePath(cNoticePath);
		
		int id = common.createBaseCode(UtilConstants.TableName.T_GROUP_NOTICE);
		
		noticeService.save(id);
		no.setNoticeID(id);
		noticeService.save(no);
		
		return null;
	}
	
	/**
	  * @descr 跳转--出团通知填写页面
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-7-6
	 */
	@RequestMapping("/GroupNotice")
	public String toGroupNotice(HttpServletRequest request, Model model) {
		
		//出团计划单的id
		String planID = request.getParameter("iPlanID");
		String lineID = request.getParameter("iLineID");
		String date = request.getParameter("cDate");
		
		model.addAttribute("planID",planID);
		model.addAttribute("lineID",lineID);
		model.addAttribute("date",date);
		
		return "webbackground/tourist/GroupNotice";
	}
	
	/**
	  * @descr 异步--出团表单提交保存
	  * @param body
	  * @return str
	  * @throws 
	  * @author liys
	  * @date 2021-5-19
	 */
	@RequestMapping("/GroupNoticePost")
	@ResponseBody
	public String noticePost(@RequestBody String ff,HttpSession session) {
		
		
		JSON.parseObject(ff,GroupForm.class);
		
		
		return "";
	}
	
	/**
	  * @descr 跳转--结账单填写页面
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-7-6
	 */
	@RequestMapping("/GroupCheck")
	public String toLineGroup3(HttpServletRequest request, Model model) {
		
		Integer.parseInt(request.getParameter("iPlanID"));
		
		return "webbackground/tourist/GroupCheck";
	}
	
	/**
	  * @descr 异步--已出团列表数据 
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-7-5
	 */
	@RequestMapping("/getGroupsByOrg")
	@ResponseBody
	public String getGroups(HttpServletRequest request,HttpSession session) {
		
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int index = (page - 1) * rows;
		int count = rows;
		int iOrgID = CodeUtil.getOrgID(session);
		List<Map<String,Object>> table = projectService.getGroups(iOrgID, count, index);
		int total = projectService.getGroupCount(iOrgID);
		Map<String,Object> ret = new HashMap<String,Object>();
		ret.put("rows", table);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}


	
}
